package com.citi.xylophone.phoneEmulator.htmlExecutor;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.Date;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.protocol.Protocol;

public class ExecutorHttpClient {

	@SuppressWarnings("deprecation")
	public String execute(HttpClient httpClient, String url,
			NameValuePair params[], String botSession) throws HttpException,
			IOException, GeneralSecurityException {
		
		////////////////////////////////
		Protocol easyhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), 443);
	    Protocol.registerProtocol("https", easyhttps);
	    /////////////////////////////////
		

		HttpMethodBase request = null;
		int status = 0;
		BufferedReader reader = null;
		String line = null;
		String result = "";

		request = new GetMethod(url);
		request.setFollowRedirects(true);
		
		if (params != null) {
			request.setQueryString(params);
		}

		String logLine = new Date() + "Robot Execute [" + botSession + "]: "
				+ url;

		boolean isFirst = true;

		if (params != null) {

			for (NameValuePair nameValuePair : params) {

				if (isFirst) {
					logLine = logLine + "?";
					isFirst = false;
				} else {
					logLine = logLine + "&";
				}
				logLine = logLine + nameValuePair.getName() + "="
						+ nameValuePair.getValue();
			}
			System.out.println(logLine);

		}

		request.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
				new DefaultHttpMethodRetryHandler(3, true));
		
		
		
		status = httpClient.executeMethod(request);
		
		//////////////////
		//HttpConnection con = new HttpConnection("127.0.0.1", 12);
		//con.setProtocol(new Protocol("easyhttps", (ProtocolSocketFactory)new EasySSLProtocolSocketFactory(), 12));
		////////////
		
		
		if (status != HttpStatus.SC_OK) {

			System.err.println("Error\t" + request.getStatusCode() + "\t"
					+ request.getStatusText() + "\t" + request.getStatusLine());

		} else {
			reader = new BufferedReader(new InputStreamReader(request
					.getResponseBodyAsStream(), request.getResponseCharSet()));
			line = reader.readLine();
			result += line;
			while (line != null) {
				result += line;
				line = reader.readLine();
			}
		}
		request.releaseConnection();

		System.out.println(new Date() + "Result Robot [" + botSession + "]: "
				+ result.replace("\n", ""));

		return result;
	}
	
	
	
}
